<?php
namespace app\admin\controller;

use think\Request;

class Deploy extends Base{

    /**
     * 轮播图
     */
    public function index(){
        $list = M('slide a')
            ->join('admin_users b','a.admin_id = b.id','left')
            ->field('a.*,b.name admin_name')
            ->order('a.id desc')->paginate('',false,page_param());
        $this->assign('list', $list);
        return $this->fetch();
    }
    //轮播图编辑
    public function index_edit(Request $request){
        if($request->isPost()){
            return $this->index_store($request);
        }
        $id = I('id',0);
        $list = M('slide')->where('id',$id)->find();
        $list['image'] = getImageUrl($list['image']);
        $this->assign('info',$list);
        return $this->fetch();
    }
    //轮播图删除
    public function index_del(){
        $params = input();
        $ids = explode(',', $params['id']);
        $title = db('slide')->where('id', 'in', $ids)->column('title');
        db('slide')->where('id', 'in', $ids)->delete();
        add_sys_log('删除轮播图:'.implode(',',$title));
        return $this->ajaxReturn(0, '成功');
    }
    //轮播图编辑或添加提交
    public function index_store(){
        $params = input();
//        var_dump($params);die;
        $result = $this->validate(
            $params,
            ['title'  => 'require|max:50',
                'image'=>'require'
            ],
            ['title.require'=>'标题必填',
                'title.max'=>'标题最多50个字符',
                'image.require'=>'图片不能为空'
            ]
        );
        if(true !== $result){
            return $this->ajaxReturn(1, $result);
        }
        if($params['id']){
            db('slide')->where('id', $params['id'])->update($params);
            add_sys_log('修改轮播图:'.$params['title']);
        }else{
            $params['admin_id'] = session('user_info')['id'];
            $params['create_time'] = time();
            db('slide')->where('id', $params['id'])->insert($params);
            add_sys_log('新增轮播图:'.$params['title']);
        }
        return $this->ajaxReturn(0,"成功", url(CONTROLLER_NAME.'/index'));
    }
    //app下载列表
    public function app_down(){
        $list = M('app_download a')
            ->join('admin_users b','a.admin_id = b.id','left')
            ->field('a.*,b.name admin_name')
            ->order('a.id desc')->paginate('',false,page_param());
        $this->assign('list', $list);
        return $this->fetch();
    }
    //app编辑
    public function app_edit(Request $request){
        if($request->isPost()){
            return $this->app_store($request);
        }
        $id = I('id',0);
        $list = M('app_download')->where('id',$id)->find();
        $this->assign('info',$list);
        return $this->fetch();
    }
    //app删除
    public function app_del(){
        $params = input();
        $ids = explode(',', $params['id']);
        $title = db('slide')->where('id', 'in', $ids)->column('name');
        db('app_download')->where('id', 'in', $ids)->delete();
        add_sys_log('删除app:'.implode(',',$title));
        return $this->ajaxReturn(0, '成功');
    }
    //app编辑或添加提交
    public function app_store(){
        $params = input();
//        var_dump($params);die;
        $result = $this->validate(
            $params,
            ['name'  => 'require|max:50',
                'image'=>'require'
            ],
            ['name.require'=>'标题必填',
                'name.max'=>'标题最多50个字符',
                'image.require'=>'图片不能为空'
            ]
        );
        if(true !== $result){
            return $this->ajaxReturn(1, $result);
        }
        if($params['id']){
            db('app_download')->where('id', $params['id'])->update($params);
            add_sys_log('修改app:'.$params['name']);
        }else{
            $params['admin_id'] = session('user_info')['id'];
            $params['create_time'] = time();
            db('app_download')->where('id', $params['id'])->insert($params);
            add_sys_log('新增app:'.$params['name']);
        }
        return $this->ajaxReturn(0,"成功", url(CONTROLLER_NAME.'/app_down'));
    }
    //系统公告列表
    public function sys_notice(){
        $list = M('sys_notice a')
            ->join('admin_users b','a.admin_id = b.id','left')
            ->field('a.*,b.name admin_name')
            ->order('a.id desc')->paginate('',false,page_param());
        $this->assign('list', $list);
        return $this->fetch();
    }
    //系统公告编辑
    public function notice_edit(Request $request){
        if($request->isPost()){
            return $this->notice_store($request);
        }
        $id = I('id',0);
        $list = M('sys_notice')->where('id',$id)->find();
        $this->assign('info',$list);
        return $this->fetch();
    }
    //系统公告删除
    public function notice_del(){
        $params = input();
        $ids = explode(',', $params['id']);
        $title = db('sys_notice')->where('id', 'in', $ids)->column('title');
        db('sys_notice')->where('id', 'in', $ids)->delete();
        add_sys_log('删除系统公告:'.implode(',',$title));
        return $this->ajaxReturn(0, '成功');
    }
    //系统公告编辑或添加提交
    public function notice_store(){
        $params = input();
//        var_dump($params);die;
        $result = $this->validate(
            $params,
            ['title'  => 'require|max:50',
                'content'=>'require'
            ],
            ['title.require'=>'标题必填',
                'title.max'=>'标题最多50个字符',
                'content.require'=>'公告内容不能为空'
            ]
        );
        if(true !== $result){
            return $this->ajaxReturn(1, $result);
        }
        if(!empty($params['end_time'])){
            $params['end_time'] = strtotime($params['end_time']);

        }
        if($params['id']){
            $create_time = db('sys_notice')->where('id', $params['id'])->value('create_time');
            if($params['end_time'] < $create_time ){
                return $this->ajaxReturn(1,'截止时间不能小于添加时间');
            }
            db('sys_notice')->where('id', $params['id'])->update($params);
            add_sys_log('修改系统公告:'.$params['title']);
        }else{
            if($params['end_time'] < time() ){
                return $this->ajaxReturn(1,'截止时间不能小于添加时间');
            }
            $params['admin_id'] = session('user_info')['id'];
            $params['create_time'] = time();
            db('sys_notice')->where('id', $params['id'])->insert($params);
            add_sys_log('新增系统公告:'.$params['title']);
        }
        return $this->ajaxReturn(0,"成功", url(CONTROLLER_NAME.'/sys_notice'));
    }
    //在线视频列表
    public function video(){
        $list = M('video a')
            ->join('admin_users b','a.admin_id = b.id','left')
            ->field('a.*,b.name admin_name')
            ->order('a.id desc')->paginate('',false,page_param());
        $this->assign('list', $list);
        return $this->fetch();
    }
    //在线视频编辑
    public function video_edit(Request $request){
        if($request->isPost()){
            return $this->video_store($request);
        }
        $id = I('id',0);
        $list = M('video')->where('id',$id)->find();
        $this->assign('info',$list);
        return $this->fetch();
    }
    //在线视频删除
    public function video_del(){
        $params = input();
        $ids = explode(',', $params['id']);
        $title = db('video')->where('id', 'in', $ids)->column('title');
        db('video')->where('id', 'in', $ids)->delete();
        add_sys_log('删除在线视频:'.implode(',',$title));
        return $this->ajaxReturn(0, '成功');
    }
    //在线视频编辑或添加提交
    public function video_store(){
        $params = input();
//        var_dump($params);die;
        $result = $this->validate(
            $params,
            ['title'  => 'require|max:50',
                'cover_img'=>'require',
                'video'=>'require',
            ],
            ['title.require'=>'标题必填',
                'title.max'=>'标题最多50个字符',
                'cover_img.require'=>'封面图片不能为空',
                'video.require'=>'视频不能为空',
            ]
        );
        if(true !== $result){
            return $this->ajaxReturn(1, $result);
        }
        if($params['id']){
            db('video')->where('id', $params['id'])->update($params);
            add_sys_log('修改在线视频:'.$params['title']);
        }else{
            $params['admin_id'] = session('user_info')['id'];
            $params['create_time'] = time();
            db('video')->where('id', $params['id'])->insert($params);
            add_sys_log('新增在线视频:'.$params['title']);
        }
        return $this->ajaxReturn(0,"成功", url(CONTROLLER_NAME.'/video'));
    }
    //在线视频修改状态
    public function video_status(){
        $params = input();
        db('video')->where('id', $params['id'])->update(['status'=>$params['status']]);
        $title = db('video')->where('id', $params['id'])->value('title');
        add_sys_log('修改在线视频状态:'.$title);
        return json_encode(['info'=>'成功', 'status'=>1]);
    }
    //服务协议
    public function server(Request $request){
        if($request->isPost()){
            return $this->server_store();
        }
        $info = M('admin_server')->find();
        $this->assign('info',$info);
        return $this->fetch();
    }
    //服务协议提交
    public function server_store(){
        $server_content = I('server_content');
        M('admin_server')->where('id',1)->update(['server_content'=>htmlspecialchars_decode($server_content)]);
        return return_json(0,'成功');
    }


}